
using System;
using System.Data;
using System.Data.SqlClient;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.Common.ValueObject;
using ERP_RORZE_TOOL.DBAccess;

namespace ERP_RORZE_ORDER.DBAccess
{

	public class OrderDAO : BaseDAO
	{
        #region Basic Method

        public static DataTable GetAll()
        {
            return ExcuteProcedure(OrderConstants.StoreProcedure.Order.GetAll);
        }

        public static DataTable GetOne(string orderId)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
						CreateParameter("@OrderID", SqlDbType.NVarChar, orderId)
                    };
                return ExcuteProcedure(OrderConstants.StoreProcedure.Order.GetOne, parameters);

            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }

        }

        public static Order LoadOne(string orderId)
        {
            Order o = new Order();
            DataTable dt = GetOne(orderId);
            if (dt.Rows.Count > 0)
            {
                //o.ID = int.Parse(dt.Rows[0][OrderConstants.Entities.Order.FieldName.ID].ToString());
                o.OrderID = dt.Rows[0][OrderConstants.Entities.Order.FieldName.OrderID].ToString();
                o.Date = DateTime.Parse(dt.Rows[0][OrderConstants.Entities.Order.FieldName.Date].ToString());
                o.Approved = int.Parse(dt.Rows[0][OrderConstants.Entities.Order.FieldName.Approved].ToString());
                o.StaffID = dt.Rows[0][OrderConstants.Entities.Order.FieldName.StaffID].ToString();

            }

            return o;
        }


        public static int Insert(Order o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        CreateParameter("@OrderID", SqlDbType.NVarChar, o.OrderID),
                        CreateParameter("@Date", SqlDbType.NVarChar, o.Date),
                        CreateParameter("@Approved", SqlDbType.NVarChar, o.Approved),
                        CreateParameter("@StaffID", SqlDbType.NVarChar, o.StaffID),
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.Order.Insert, parameters);

            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }

        }

        public static int Update(Order o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        CreateParameter("@TempID", SqlDbType.NVarChar, o.TempId),
                        CreateParameter("@OrderID", SqlDbType.NVarChar, o.OrderID),
                        CreateParameter("@Date", SqlDbType.NVarChar, o.Date),
                        CreateParameter("@Approved", SqlDbType.NVarChar, o.Approved),
                        CreateParameter("@StaffID", SqlDbType.NVarChar, o.StaffID),
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.Order.Update, parameters);
            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }
        }

        public static int Delete(Order o)
        {
            try
            {
                var parameters = new SqlParameter[]
                    {
                        CreateParameter("@OrderID", SqlDbType.NVarChar, o.OrderID)
                    };

                return ExecuteNonQueryProcedure(OrderConstants.StoreProcedure.Order.Delete, parameters);
            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }
        }

        public static DataTable GetTemplateTable()
        {
            DataTable table = new DataTable("243_Order");

            return table;
        }


        #endregion
    }
	
}
